package cn.edu.nju.software.zp13.core2.xmlNodeEntity;

import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import cn.edu.nju.software.zp13.core2.annotationParser.AnnotationParser;
import cn.edu.nju.software.zp13.core2.nodeDescriptor.NodeDescriptor;
import cn.edu.nju.software.zp13.core2.nodeGenerator.NodeGenerator;
import cn.edu.nju.software.zp13.core2.nodeGenerator.factory.NodeGeneratorFactory;

public class XmlNodeEntity {
	/**
	 * return a Node which contains all infomation that this Object wants to
	 * expose.
	 * 
	 * @param dom
	 *            org.w3c.dom.Document
	 * @return org.w3c.dom.Node
	 */
	public final Node toXmlNode(Document dom) {
		String rootNodeTagName = AnnotationParser.getTagName(this);
		Element root = dom.createElement(rootNodeTagName);
		List<NodeDescriptor> nodeDescriptors=AnnotationParser.generateNodeDescriptors(this);
		nodeDescriptors.forEach(nodeDescriptor->{
			NodeGenerator ng=NodeGeneratorFactory.createNodeGenerator(dom, nodeDescriptor);
			ng.updateDom(root, nodeDescriptor);
		});
		return root;
	}
}
